package com.zy.mc_module.base.kotlin

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.collection.SparseArrayCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.zy.mc_module.utils.NoDoubleClickListener
import com.zy.mc_module.utils.StringsUtils

abstract class BaseAdapter<VB : ViewBinding, T>(private var mData: MutableList<T>) : RecyclerView.Adapter<BaseViewHolder<VB>>() {
    var TAG = javaClass.simpleName
    private var itemClickListener: OnItemClickListener? = null
    private var itemLongClickListener: OnItemLongClickListener? = null

    val CONTENT_VIEW = 0x00000111 //主视图

    val HEADER_VIEW = 0x00000222 //头部视图

    val FOOTER_VIEW = 0x00000333 //尾部视图

    val EMPTY_VIEW = 0x00000555 //空视图


    /***头部视图 */
    private val mHeaderViews = SparseArrayCompat<ViewBinding>()

    /***尾部视图 */
    private val mFootViews = SparseArrayCompat<ViewBinding>()

    /***empty */
    private var mEmptyLayout: FrameLayout? = null

    abstract fun getViewBinding(inflater: LayoutInflater, parent: ViewGroup): VB

    override fun getItemCount(): Int {
        return getHeadersCount() + getFootersCount() + getRealItemCount()
    }

    /***
     * 判断当前item类型
     */
    override fun getItemViewType(position: Int): Int {
        if (isHeaderViewPos(position)) {
            //头部
            return mHeaderViews.keyAt(position)
        } else if (isFooterViewPos(position)) {
            //尾部
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount())
        }
        return CONTENT_VIEW
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
        //空视图
        if (viewType == EMPTY_VIEW) {
            return BaseViewHolder(getViewBinding(LayoutInflater.from(parent.context), parent))
        }
        //主布局
        var vb = getViewBinding(LayoutInflater.from(parent.context), parent)
        val itemView = vb.root
        //单击事件
        itemView.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View) {
                if (itemClickListener != null) {
                    itemClickListener!!.onItemClick(v.tag as Int)
                }
            }
        })
        itemView.setOnLongClickListener { v: View ->
            if (itemLongClickListener != null) {
                itemLongClickListener!!.onItemLongClick(v.tag as Int)
            }
            true
        }
        if (null != mHeaderViews[viewType]) {
            return BaseViewHolder(getHeaderView(viewType))
        } else if (null != mFootViews[viewType]) {
            return BaseViewHolder(getFooterView(viewType))
        }
        return BaseViewHolder(vb)
    }

    override fun onBindViewHolder(holder: BaseViewHolder<VB>, position: Int) {
        if (null == mEmptyLayout) {
            //holder.itemView.setTag(position);
            //onMyBindViewHolder(holder, position);

            //如果是HeaderView 或者是 FooterView 不进行绑定数据,因为是从外部传进来的,不由列表更新
            if (isHeaderViewPos(position) || isFooterViewPos(position)) {
                return
            }
            holder.itemView.tag = position - getHeadersCount()
            onMyBindViewHolder(holder, position - getHeadersCount())
        }
    }

    protected abstract fun onMyBindViewHolder(holder: BaseViewHolder<VB>, position: Int)

    /***
     * 点击事件接口
     */
    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    /***
     * 长按事件接口
     */
    interface OnItemLongClickListener {
        fun onItemLongClick(position: Int)
    }

    open fun setItemClickListener(itemClickListener: OnItemClickListener?) {
        this.itemClickListener = itemClickListener
    }

    open fun setItemLongClickListener(itemLongClickListener: OnItemLongClickListener?) {
        this.itemLongClickListener = itemLongClickListener
    }

    open fun getView() {}

    //-----------------------------
    open fun getList(): List<T> {
        return mData
    }

    /**
     * 设置数据
     */
    open fun setData(data: MutableList<T>?) {
        if (data == null) {
            return
        }
        mData = data
        notifyDataSetChanged()
    }

    open fun add(item: T) {
        mData.add(item)
        notifyItemInserted(mData.size - 1)
    }

    open fun add(position: Int, item: T) {
        mData.add(position, item)
        notifyItemInserted(position)
    }

    open fun addAll(list: MutableList<T>) {
        if (list != null) {
            val positon = mData.size + getHeadersCount()
            mData.addAll(list)
            notifyItemRangeInserted(positon, list.size)
        }
    }

    open fun setAll(list: MutableList<T>) {
        mData.clear()
        mData.addAll(list)
        notifyDataSetChanged()
    }

    open fun removeLast() {
        if ( mData.size > 0) {
            mData.removeAt(mData.size - 1)
            notifyItemRemoved(mData.size - 1)
        }
    }

    open fun remove(position: Int) {
        mData.removeAt(position)
        // notifyItemRemoved(position);
        notifyDataSetChanged()
    }

    open fun removeAll() {
        mData.clear()
        notifyDataSetChanged()
    }

    open fun update(position: Int, item: T) {
        mData.removeAt(position)
        mData.add(position, item)
        notifyItemChanged(position)
    }

    /***
     * 刷新指定位置数据
     */
    open fun updatePos(position: Int) {
        notifyItemChanged(position)
    }

    /***
     * 设置空视图
     */
    open fun setEmptyView(layoutResId: Int, viewGroup: ViewGroup) {
        val view = LayoutInflater.from(viewGroup.context).inflate(layoutResId, viewGroup, false)
        setEmptyView(view)
    }


    /***
     * 判断当前位置是否是头部
     */
    private fun isHeaderViewPos(position: Int): Boolean {
        return position < getHeadersCount()
    }

    /***
     * 判断当前位置是否是尾部
     */
    private fun isFooterViewPos(position: Int): Boolean {
        return position >= getHeadersCount() + getRealItemCount()
    }

    /***
     * 设置空视图
     */
    open fun setEmptyView(emptyView: View) {
        var insert = false
        if (mEmptyLayout == null) {
            mEmptyLayout = FrameLayout(emptyView.context)
            val layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT)
            val lp = emptyView.layoutParams
            if (lp != null) {
                layoutParams.width = lp.width
                layoutParams.height = lp.height
            }
            mEmptyLayout!!.layoutParams = layoutParams
            insert = true
        }
        mEmptyLayout!!.removeAllViews()
        mEmptyLayout!!.addView(emptyView)
        //mIsUseEmpty = true;
        if (insert) {
            notifyItemInserted(0)
        }
    }

    /***
     * 主视图数量
     */
    open fun getRealItemCount(): Int {
        return if (StringsUtils.isEmpty(mData) && null != mEmptyLayout) {
            1
        } else {
            mData.size
        }
        //return mData == null ? 0 : mData.size();
    }

    /***
     * 获取头部视图
     */
    open fun getHeadersCount(): Int {
        return mHeaderViews.size()
    }

    /***
     * 获取尾部视图
     */
    private fun getFootersCount(): Int {
        return mFootViews.size()
    }

    fun <V : ViewBinding> getHeaderView(viewType: Int): V {
        return mHeaderViews[viewType] as V
    }

    fun <V : ViewBinding> getFooterView(viewType: Int): V {
        return mFootViews[viewType] as V
    }

    /***
     * 添加头部
     * @param view
     */
    open fun <V : ViewBinding> addHeaderView(viewBinding: V?) {
        mHeaderViews.put(mHeaderViews.size() + HEADER_VIEW, viewBinding)
    }

    /***
     * 添加尾部
     * @param view
     */
    open fun addFootView(viewBinding: ViewBinding?) {
        mFootViews.put(mFootViews.size() + FOOTER_VIEW, viewBinding)
    }
}